package com.blrs.controller;

import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.core.page.TableDataInfo;
import com.blrs.common.enums.BusinessType;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.domain.TeacherInfoNew;
import com.blrs.service.teacherInfo.ITeacherInfoNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/teacher/info/new")
public class TeacherInfoNewController extends BaseController {
    
    @Autowired
    private ITeacherInfoNewService teacherInfoService;

    @GetMapping("/list")
    public TableDataInfo list(TeacherInfoNew teacherInfo) {
        startPage();
        List<TeacherInfoNew> list = teacherInfoService.selectTeacherInfoList(teacherInfo);
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(teacherInfoService.selectTeacherInfoById(id));
    }

    @PostMapping
    public AjaxResult add(@RequestBody TeacherInfoNew teacherInfo) {
        return toAjax(teacherInfoService.insertTeacherInfo(teacherInfo));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody TeacherInfoNew teacherInfo) {
        return toAjax(teacherInfoService.updateTeacherInfo(teacherInfo));
    }

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(teacherInfoService.deleteTeacherInfoByIds(ids));
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, TeacherInfoNew teacherInfo) {
        List<TeacherInfoNew> list = teacherInfoService.selectTeacherInfoList(teacherInfo);
        ExcelUtil<TeacherInfoNew> util = new ExcelUtil<>(TeacherInfoNew.class);
        util.exportExcel(response, list, "师资信息数据");
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TeacherInfoNew> util = new ExcelUtil<>(TeacherInfoNew.class);
        List<TeacherInfoNew> teacherInfoList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = teacherInfoService.importTeacherInfo(teacherInfoList, updateSupport, operName);
        return success(message);
    }

    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TeacherInfoNew> util = new ExcelUtil<>(TeacherInfoNew.class);
        util.importTemplateExcel(response, "师资信息数据");
    }
}